# coding=utf8
# https://jira.readthedocs.io/en/latest/jirashell.html
import pymysql
from jira import JIRA
# from . import config
import time
import sys
import datetime
host="10.1.62.123"
user="root"
password="Auto_123"
db="jira"
port=3306
from concurrent import futures
import logging

logger = logging.getLogger(__name__)




jira_url='http://jira.uyunsoft.cn'
basic_user='jira_tester'
basic_passwd='Broada@123!'

# jira = JIRA(config.jira_url, basic_auth=(config.basic_user, config.basic_passwd))
# connection = pymysql.connect(host=config.host, user=config.user,
#                          password=config.password, db=config.db, port=config.port)

jira = JIRA(jira_url, basic_auth=(basic_user, basic_passwd))
connection = pymysql.connect(host=host, user=user,
                         password=password, db=db, port=port,charset='UTF8')

# issues = jira.search_issues('project = servicepatrol')
# print (issues)
today = datetime.date.today()
issues = jira.search_issues(
    '(summary !~  "\\"测试总单\\""  AND summary !~  "\\"测试问题总单\\"" AND summary !~  "\\"测试任务总单\\"" and summary !~ "\\"缺陷总单\\"" ) and '
    '(updatedDate>="{}") and (project!=CPJF)'.format(today), maxResults=False)
for task in issues:
    try:
        issue_key = task.fields.parent
        # print(issue_key)
        displayName = task.fields.reporter.displayName if task.fields.reporter.displayName else '未填写'
        print(displayName)
    except:
        logger.debug("+++++ error issue_key: {}".format(issue_key))
        print(issue_key,task.key)
        sys.exit(1)


# issue_info = jira.issue('SER-282')
# tasks = issue_info.fields.subtasks
# for task in tasks:
#     print (task.key)
    # for field_name in task.raw['fields']:
    #     print("Field:", field_name, "Value:", task.raw['fields'][field_name])
# print (issue_info.fields)
# print (issue_info)
# for field_name in issue_info.raw['fields']:
#     print ("Field:", field_name, "Value:", issue_info.raw['fields'][field_name])


# def get_issues_today(today=None):
#     connection = pymysql.connect(host=host, user=user,
#                                  password=password, db=db, port=port, charset='UTF8')
#     cursor = connection.cursor()
#     if not today:
#         today = datetime.date.today()
#     issues = jira.search_issues('(summary ~  "\\"测试总单\\""  OR summary ~  "\\"测试问题总单\\"" '
#                                 'OR summary ~  "\\"缺陷总单\\"" OR summary ~  "\\"测试任务总单\\"" )and '
#                                 '(updatedDate>="{}")'.format(today), maxResults=False)
#
#     for issue in issues:
#         issue_info = jira.issue(issue)
#         cmd = "delete from dashboard_issues where code='{}';".format(issue_info.key)
#         cursor.execute(cmd)
#         connection.commit()
#
#         if issue_info.fields.fixVersions:
#             version = issue_info.fields.fixVersions[0].name.replace("'", "`")
#         else:
#             version = '无'
#         if issue_info.fields.assignee:
#             assignee = issue_info.fields.assignee.displayName
#         else:
#             assignee = '未分配'
#
#         cmd = "insert ignore into dashboard_issues values(NULL, '{}','{}','{}','{}','{}','{}','{}','{}',1 );".format(
#             issue_info.key,
#             issue_info.fields.reporter.displayName,
#             assignee,
#             issue_info.fields.project.key,
#             issue_info.fields.project.name,
#             version,
#             issue_info.fields.summary.replace("'", "`"),
#             issue_info.fields.status.name
#         )
#         cursor.execute(cmd.encode('utf8'))
#     connection.commit()
#     connection.close()
